#ifndef SCOPEDLOCK_H
#define SCOPEDLOCK_H

#include <string.h>
#include "NoCopy.h"

// RAII lock guard.
// Lockable must have lock and unlock methods.

template<typename Lockable>
class ScopedLock : NoCopy {
public:
	explicit ScopedLock(Lockable *obj) : obj_(obj) {
		if (obj_ != NULL)
			obj_->lock();
	}
	
	~ScopedLock() {
		if (obj_ != NULL)
			obj_->unlock();
	}
	
private:
	ScopedLock();
	
	Lockable *obj_;
};

#endif
